package com.chainyoung.component_tmtrade.ui.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chainyoung.common.utils.ConvertUtils;
import com.chainyoung.common.utils.ScreenUtils;
import com.chainyoung.component_tmtrade.R;
import com.chainyoung.component_tmtrade.R2;
import com.chainyoung.component_tmtrade.bean.FiatCoin;
import com.chainyoung.component_tmtrade.ui.adapter.SelectOrderStatusAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class SelectCoinWindow extends PopupWindow {


    @BindView(R2.id.rv_list)
    RecyclerView rvList;


    private Context mContext;
    private Unbinder unbinder;
    private CoinListAdapter adapter;
    private List<FiatCoin> mCoinList = new ArrayList<>();
    private OnSelectCoin onSelectCoin;


    public SelectCoinWindow(Context context, List<FiatCoin> data) {
        this.mContext = context;
        View windowView = LayoutInflater.from(mContext).inflate(R.layout.layout_select_coin_window, null);
        setContentView(windowView);
        unbinder = ButterKnife.bind(this, windowView);
        int appWidth = ScreenUtils.getScreenWidth(mContext);
        this.setWidth(ConvertUtils.dp2px(mContext,86));
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setOutsideTouchable(true);
        this.setFocusable(true);
        this.setBackgroundDrawable(new BitmapDrawable());
        this.mCoinList = data;
        this.setAnimationStyle(R.style.PopupAnimation);
        initView();
    }

    private void initView() {
        rvList.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new CoinListAdapter(mCoinList);
        rvList.setAdapter(adapter);
        adapter.notifyDataSetChanged();
    }


    //显示
    public void showPopupWindow(View parent) {
        if (!this.isShowing()) {
            this.showAsDropDown(parent, 0, 4);
        } else {
            this.dismiss();
        }
    }

    @Override
    public void showAsDropDown(View anchor) {
        if (!this.isShowing()) {
            backgroundAlpha(0.5f);
            super.showAsDropDown(anchor);
        } else {
            dismiss();
        }

    }

    @Override
    public void showAsDropDown(View anchor, int xoff, int yoff) {
        if (!this.isShowing()) {
            backgroundAlpha(0.5f);
            super.showAsDropDown(anchor, xoff, yoff);
        } else {
            dismiss();
        }
    }


    public OnSelectCoin getOnSelectCoin() {
        return onSelectCoin;
    }

    public void setOnSelectCoin(OnSelectCoin onSelectCoin) {
        this.onSelectCoin = onSelectCoin;
    }

    public interface OnSelectCoin {
        void onSelectCoin(FiatCoin fiatCoin);
    }

    private class CoinListAdapter extends BaseQuickAdapter<FiatCoin, BaseViewHolder> {

        public CoinListAdapter(@Nullable List<FiatCoin> data) {
            super(R.layout.layout_select_fiat_coin_item, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, final FiatCoin item) {
            helper.setText(R.id.tv_coin_name, item.getKey().toUpperCase());
            helper.getView(R.id.ll_coin).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onSelectCoin != null) {
                        onSelectCoin.onSelectCoin(item);
                    }
                }
            });
        }
    }


    @Override
    public void dismiss() {
        backgroundAlpha(1f);
        super.dismiss();
    }

    /**
     * 设置添加屏幕的背景透明度
     */
    private void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = ((Activity) mContext).getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        ((Activity) mContext).getWindow().setAttributes(lp);
    }

}






